package com.cskaoyan.util;

import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketUser;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @author 楚夏三分
 * @datetime 2023/1/2 16:37
 * @description: TODO:
 */
@Data
public class SubjectUtil {
    /**
     * TODO: 获取用户信息PrimaryPrincipal
     * TODO：调用前 需要验证登录状态
     * TODO：游客登录,不可调用
     */
    public static <T> T getUserInfo(T user) {
        Subject subject = SecurityUtils.getSubject();
        //判断当前登录用户类型
        String type = (String) subject.getSession().getAttribute("userType");
        if ("admin".equals(type)) {
            return (T) subject.getPrincipals().getPrimaryPrincipal();
        } else if ("wx".equals(type)) {
            return (T) subject.getPrincipals().getPrimaryPrincipal();
        }
        return null;
    }

    /**
     * TODO：验证登录状态
     * TODO：true 已登录,false 未登录
     */
    public static boolean isLogin() {
        Subject subject = SecurityUtils.getSubject();
        //判断当前登录用户类型
        String type = (String) subject.getSession().getAttribute("userType");
        if ("admin".equals(type)) {
            MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
            return !StringUtil.isEmpty(marketAdmin.getUsername());
        } else if ("wx".equals(type)) {
            MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            return !StringUtil.isEmpty(marketUser.getUsername());
        }
        return false;
    }

}
